/*
 * Copyright (c) 2014 pkzd/1.0, 颜文泽(robin)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     1 Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     2 Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef MC_TTY_H
#define MC_TTY_H

#include	<types.h>
#include	<param.h>

#define NCNS	8
#define CBSIZE	32	
#define NCCS	0x10
#define TTYHOG	400	
#define NCLIST	(PAGESIZ / CBSIZE)

#define KLSRT	0x64
#define KLDAT	0x60
#define KLDIB	0x01

#define XTABS	0x01
#define ECHO	0x10

#define ISOPEN	0x4
#define CARR_ON	0x20
#define BUSY	0x40
#define ASLEEP	0x100

#define CAPLOCK	0x1
#define SHIFT	0x2
#define CTRL	0x4
#define ALT	0x10

#define Ctrl(x)	((x) - '@')

#define VBRK	0x0
#define VEOF	0x1
#define VERASE	0x2
#define VKILL	0x3
#define VINTR	0x4
#define VQUIT	0x5
#define VSTART 	0x6 
#define VSTOP	0x7
#define VCLEAR	0x8

#define CBRK	256	
#define CEOT	Ctrl('D')
#define CERASE	Ctrl('H')	
#define CR	Ctrl('M')
#define CNL	Ctrl('J')
#define CTAB	Ctrl('I')
#define CKILL	Ctrl('U')
#define CINTR	Ctrl('C')
#define CQUIT	Ctrl('\\')
#define CSTART	Ctrl('Q')
#define CSTOP	Ctrl('S')
#define CLEAR	Ctrl('L')

struct cblock{
	struct cblock *c_next;
	struct cblock *c_back;
	int	c_info[6];
};

struct clist{
	uint	c_cc;
	int	*c_cf;
	int	*c_cl;
};

struct tty{
	dev_t	t_dev;
	uint	t_pos;
	pid_t	t_pgrp;
	uint	t_addr;
	uint	t_mode;
	uint	t_state;
	uint	t_flags;
	uint	t_delct;
	char	t_cc[NCCS];
	struct clist t_rawq;
	struct clist t_canq;
	struct clist t_outq;
};
	
extern struct tty cns[];
extern struct cblock *cbuf;
extern struct cblock *cfreelist;

int	cinit(void);
void	cfree(struct cblock *cp);
int	getc(struct clist *cp);
int	unputc(struct clist *cp);
void	ttychars(struct tty *tp);
void	flushtty(struct tty *atp);
int	putc(struct clist *cp, int c);
int	canon(struct tty *atp);
void	ttyinput(int ac, struct tty *atp);
struct cblock	*calloc(void);

void 	cnsint(void);
int	cnsclose(dev_t dev);
int	cnsopen(dev_t dev, uint flag);
int	cnsread(dev_t dev, void *buf, size_t cnt);
int	cnswrite(dev_t dev, const void *buf, size_t cnt);
void	cnsclear(struct tty *tp);

#endif
